package com.investmenttechnologyservices.model;

import java.util.HashMap;
import java.util.Map;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;


public class Region extends EntityHolder{
	//String id;
	//String name;
	//Integer dow = 62; // 0111110 Mon - Fri (Sun - Thur 31, Sun - Sat 127)
	//Key parentKey (hiearchy of regions)
	//Integer defaultSettlementDays
	//Map<InstrumentType, Integer> instrumentTypeSettlementDays

	private static final Long mondayToFriday = 62L;
	private static final Integer standardSettlementDays = 3;

	public Region(Entity entity) {
		super(entity);
	}
	
	public Region(String id) {
	    super("region", id);
	}

	public Region(String id, String name, Long dow, Key parentKey, 
			Integer defaultSettlementDays, 
			Map<InstrumentType, Integer> instrumentTypeSettlementDays) {
		this(id);
		setName(name);
		setDow(dow);
		setParentKey(parentKey);
		setDefaultSettlementDays(defaultSettlementDays);
		setInstrumentTypeSettlementDays(instrumentTypeSettlementDays);
	}

	public Region(String id, String name, Long dow, Key parentKey) {
		this(id, name, dow, parentKey, standardSettlementDays, null );
	}
	
	public Region(String id, String name, Key parentKey) {
		this(id, name, mondayToFriday, parentKey);
	}

	public Region(String id, String name, Long dow) {
		this(id, name, dow, null);
	}
	
	public Region(String id, String name) {
		this(id, name, mondayToFriday);
	}

	public Key getKey() {return super.getKey();}
	public String getId() {return super.getId();}
	
	public String getName() {
		return (String) getProperty("name");
	}

	public void setName(String name) {
		setProperty("name", name);
	}
	
	public Long getDow() {
		Long dow = (Long) getProperty("dow");
		if (dow == null) {
			dow = mondayToFriday;
		}
		return dow;
	}

	public void setDow(Long dow) {
		setUnindexedProperty("dow", dow);
	}
/**
 * getParentKey()	
 * @return Key
 * 
 * Returns the entity key value. Use getName() to determine the value.
 */
	public Key getParentKey() {
		return (Key)getProperty("parentKey");
	}
/**
 * setParentKey	
 * @param parentKey
 * Sets the parent of the region to another region (not part of the datastore key)
 */
	public void setParentKey(Key parentKey) {
		setUnindexedProperty("parentKey",parentKey);
	}

	public void setDefaultSettlementDays(Integer defaultSettlementDays) {
		if (defaultSettlementDays != standardSettlementDays) {
			setUnindexedProperty("defaultSettlementDays",defaultSettlementDays,true);
		}
	}
	public Integer getDefaultSettlementDays() {
		Integer defaultSettlementDays = (Integer)getProperty("defaultSettlementDays");
		if (defaultSettlementDays == null) {
			defaultSettlementDays = standardSettlementDays;
		}
		return defaultSettlementDays;
	}
	public void setInstrumentTypeSettlementDays(Map<InstrumentType, Integer> instrumentTypeSettlementDays) {
		if(instrumentTypeSettlementDays!= null && !instrumentTypeSettlementDays.isEmpty()) {
			Map<String, Integer> storableValues = new HashMap<String, Integer>();
			for (Map.Entry<InstrumentType, Integer> entry : instrumentTypeSettlementDays.entrySet()) {
				storableValues.put(entry.getKey().name(),entry.getValue());
			};
			setUnindexedProperty("instrumentTypeSettlementDays",storableValues);
		}
	}
	public Map<InstrumentType, Integer> getInstrumentTypeSettlementDays(){
		@SuppressWarnings("unchecked")
		Map<String, Integer> storedValues = 
				(Map<String, Integer>)getProperty("instrumentTypeSettlementDays");
		if (storedValues == null || storedValues.isEmpty()) {
			return null;
		} else {
			Map<InstrumentType, Integer> instrumentTypeSettlementDays = new HashMap<InstrumentType, Integer>();
			for (Map.Entry<String, Integer> entry : storedValues.entrySet()) {
				instrumentTypeSettlementDays.put(InstrumentType.valueOf(entry.getKey()),entry.getValue());
			};
			return instrumentTypeSettlementDays;
		}
	}

	
}
